package panel;


import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
import java.util.Vector;
import libs.Normalization;
import libs.Utils;



public class PanelFeatureMap extends JPanel {
	
	private static final long serialVersionUID = 1L;
	
	public PanelFeatureMap(){
		super();
		
	}

    public int selectedClassLabels=0;
    
    
	public void paint(Graphics g){
		if (originalFeatureBands==null) {
			return;
		}
        g.setColor(Color.white);
        g.fillRect(0, 0,getWidth(),getHeight());
		int n=originalFeatureBands.length;
        int left=80;
        int top=0;
    	g.setColor(Color.black);
        g.drawString("Discrimination Power of Spectral Bands (Band vs Quantization)",getWidth()/2-150,top-20);		
//        int delta=(getWidth()-20)/n;
        int delta=28;
        originalNormalizedFeatureWeights=originalFeatureWeights.clone();
        if (type==0) {
        	normalizeValuesMinMax(originalNormalizedFeatureWeights);
		}
        
        rankedNormalizedFeatureWeights=rankedFeatureWeights.clone();
        normalizeValuesMinMax(rankedNormalizedFeatureWeights);
//        list(originalNormalizedFeatureWeights);
        if (QLevel==1) {
            left=10;
        	g.setColor(Color.LIGHT_GRAY);
            g.fillRect(0, 0,getWidth(),getHeight());		
        	g.setColor(Color.black);
        	
//        	Utils.drawVerticalText(g,"Merhaba",2,getHeight()/2,getWidth(),getHeight());
        	
//        	g.drawString("Q1",2,getHeight()/2);
        	int height=getHeight()-30;
//        	int q=height/2;
        	int q=height;
//            int t1=150;
//            int t2=50;
//            int t3=50;
        	for (int i = 0; i < n; i++) {
            	int h=(int)Math.round(Double.parseDouble(originalNormalizedFeatureWeights[i])*q);
            	g.setColor(Color.black);
            	g.drawString(originalFeatureBands[i], left+delta*i,getHeight()-2);
            	g.fillRect(left+delta*i, (getHeight()-h), delta, h-15);
            	g.setColor(Color.white);
            	g.drawRect(left+delta*i, (getHeight()-h), delta, h-15);
//            	g.setColor(Color.red);
//            	g.drawString(originalNormalizedFeatureWeights[i], 5+left+delta*i,getHeight()-h-5);
            	
        	}
            if (type==0) {
            	for (int i = 0; i < selectedClassLabels; i++) {
    				int index=getOriginalIndex(Integer.parseInt(rankedFeatureIndex[i]),originalFeatureIndex);
                	double rWeight=Double.parseDouble(originalNormalizedFeatureWeights[index]);
    				int h=(int)Math.round(rWeight*q);
                	g.setColor(Color.green);
                	g.fillRect(left+delta*index, getHeight()-h, delta, h-15);
                   	g.setColor(Color.orange);
                	g.drawRect(left+delta*index, getHeight()-h, delta, h-15);
                	g.setColor(Color.black);
                	g.drawString(originalFeatureWeights[index]+"", 2+left+delta*index,getHeight()-h);
                	g.setColor(Color.black);
                	g.drawString((i+1)+"", 2+left+delta*index,getHeight()-h/2);
    				
    			}
			}
            if (type==2) {
				q=20;
            	String[] bands=selBands.split(";");
				for (int i = 0; i < bands.length; i++) {
    				int index=Integer.parseInt(bands[i]);
                 	int h=(int)Math.round(Double.parseDouble(originalNormalizedFeatureWeights[index])*q);
                	g.setColor(Color.green);
                	g.fillRect(left+delta*index, getHeight()-h, delta, h-15);
                   	g.setColor(Color.orange);
                	g.drawRect(left+delta*index, getHeight()-h, delta, h-15);
                	g.setColor(Color.blue);
                	g.drawString((i+1)+"", 5+left+delta*index,h-70);
				}
			}
		}else{
		}
    	g.setColor(Color.black);
        //g.drawString("Spectral Bands",getWidth()/2-50,getHeight()-5);		
        
     }

	private int getOriginalIndex(int i, String[] r) {
		for (int j = 0; j < r.length; j++) {
			if (r[j].equals(i+"")) {
				return j;
			}
		}
		return 0;
	}

	@SuppressWarnings("unchecked")
	private void normalizeValuesMinMax(String[] obj) {
		double min=Utils.getMin(obj);
		double max=Utils.getMax(obj);
		if (min==max) {
			for (int i = 0; i < obj.length; i++) {
				obj[i]=""+(Double.parseDouble(obj[i])*10);
			}
			return;
		}
		Vector data=new Vector();
		for (int i = 0; i < obj.length; i++) {
			data.add(obj[i]);
		}
		Normalization norm=new Normalization();
		data=norm.getMinMax(data);
		for (int i = 0; i < obj.length; i++) {
			obj[i]=(Double.parseDouble(data.get(i).toString())+0.05)+"";
//			System.out.println("weight:"+obj[i]);
		}
	}

	public void setSelectedClassLabels(int n) {
		selectedClassLabels=n;
		this.updateUI();
		
	}
	
	private int type=0;
	private String selBands="";
	
	public void setSelectedClassLabels(int type,int n,String bands) {
		this.type=type;
		this.selBands=bands;
		selectedClassLabels=n;
		this.updateUI();
		
	}

	private int QLevel=1;
	private String[] rankedFeatureWeights;
	private String[] rankedFeatureBands;
	private String[] rankedFeatureIndex;
	private String[] rankedNormalizedFeatureWeights;
	private String[] originalFeatureIndex;
	private String[] originalFeatureWeights;
	private String[] originalNormalizedFeatureWeights;
	private String[] originalFeatureBands;
	
	public String[] getOriginalFeatureIndex(){
		return this.originalFeatureIndex;
	}
	
	public void setVal(int qlevel, String[] rnkFWeights, String[] xval, String[] rankedIndex) {
		QLevel=qlevel;
		rankedFeatureWeights=rnkFWeights.clone();
		rankedFeatureBands=xval;
		rankedFeatureIndex=rankedIndex.clone();	
		originalFeatureIndex=rankedFeatureIndex.clone();
		//list(originalFeatureIndex);
		originalFeatureWeights=rankedFeatureWeights.clone();
		originalFeatureBands=rankedFeatureBands.clone();
		getOriginalOrder(originalFeatureBands,originalFeatureWeights,originalFeatureIndex);
	}
	
//	private void list(String[] obj) {
//		for (int i = 0; i < obj.length; i++) {
//			System.out.println(i+". "+obj[i]);
//		}		
//	}

	private void getOriginalOrder(String[] rnkb, String[] rnkw, String[] index) {
		//first we will try to sort index in an ascending order
		int temp=0;
		int n=index.length;
		for (int i = 0; i < n; i++) {
			for (int j = i; j < n; j++) {
				int ii=Integer.parseInt(index[i]);
				int jj=Integer.parseInt(index[j]);
				if (jj<ii) {
					temp=ii;
					index[i]=index[j];
					index[j]=temp+"";
					
					String t="";
					t=rnkb[i];
					rnkb[i]=rnkb[j];
					rnkb[j]=t;
					
					t=rnkw[i];
					rnkw[i]=rnkw[j];
					rnkw[j]=t;					
				}				
			}
		}
	}

}  